<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">
<ui:composition template="template.xhtml">
	<ui:define name="content">
		<h:form id="form01">
			<p:growl id="messages" />
			<p:panel header="Treatment"
				rendered="#{mainMenuControler.hasUserRole('admin') || mainMenuControler.hasUserRole('treatment')}"
				style="text-align:center">
				<p:dataTable id="patientTable" widgetVar="patientTable_w"
					resizableColumns="true" style="margin-bottom:20px"
					value="#{treatmentControler.patientList}" var="patient"
					filteredValue="#{treatmentControler.filteredPatients}">
					<p:column style="width:10%" filterBy="#{patient.seq}"
						filterMatchMode="contains"
						headerText="#{dictionaryControler.dictionary['Seq']}">
						<h:outputText value="#{patient.seq}" />
					</p:column>
					<p:column style="width:10%" filterBy="#{patient.name}"
						filterMatchMode="contains"
						headerText="#{dictionaryControler.dictionary['Name']}">
						<h:outputText value="#{patient.name}" />
					</p:column>
					<p:column style="width:10%" filterMatchMode="exact"
						filterBy="#{patient.gender}"
						headerText="#{dictionaryControler.dictionary['Gender']}">
						<f:facet name="filter">
							<p:selectOneMenu onchange="PF('patientTable_w').filter()">
								<f:selectItem itemLabel="Select One" itemValue="#{null}"
									noSelectionOption="true" />
								<f:selectItems value="#{genderListControler.genderList}"
									var="gender" itemLabel="#{gender.label}"
									itemValue="#{gender.value}" />
							</p:selectOneMenu>
						</f:facet>
						<h:outputText
							value="#{dictionaryControler.dictionary[patient.genderName]}" />
					</p:column>
					<p:column style="width:10%"
						headerText="#{dictionaryControler.dictionary['Age']}">
						<h:outputText value="#{patient.age}" />
					</p:column>
					<p:column style="width:10%" filterBy="#{patient.phoneNumber}"
						filterMatchMode="contains"
						headerText="#{dictionaryControler.dictionary['PhoneNumber']}">
						<h:outputText value="#{patient.phoneNumber}" />
					</p:column>
					<p:column style="width:3%"
						headerText="#{dictionaryControler.dictionary['View']} #{dictionaryControler.dictionary['Record']}">
						<p:commandButton
							action="#{treatmentControler.startViewTreatmentRecord}" update=":form01:treatmentRecordTable"
							icon="ui-icon-pencil">
							<f:setPropertyActionListener value="#{patient}"
								target="#{treatmentControler.selectedPatient}" />
						</p:commandButton>
					</p:column>
					<p:column style="width:3%"
						headerText="#{dictionaryControler.dictionary['Diagnose']}">
						<p:commandButton
							action="#{treatmentControler.startAddTreatmentRecord}"
							update=":form04:detailDialog" icon="ui-icon-pencil">
							<f:setPropertyActionListener value="#{patient}"
								target="#{treatmentControler.selectedPatient}" />
						</p:commandButton>
					</p:column>
					<p:column style="width:3%"
						headerText="#{dictionaryControler.dictionary['Delete']}">
						<p:commandButton ajax="true"
							oncomplete="PF('patientTable_w').clearFilters();"
							action="#{treatmentControler.deletePatient}"
							update=":form01:patientTable" icon="ui-icon-close"
							style="margin-right:5px">
							<f:setPropertyActionListener value="#{patient}"
								target="#{treatmentControler.selectedPatient}" />
							<p:confirm
								header="#{dictionaryControler.dictionary['Confirmation']}"
								message="#{dictionaryControler.dictionary['Are you sure']}?"
								icon="ui-icon-alert" />
						</p:commandButton>
					</p:column>
				</p:dataTable>
				<p:messages id="message" />
				<p:dataTable id="treatmentRecordTable" widgetVar="treatmentRecord_w"
					value="#{treatmentControler.treatmentRecordList}"
					var="treatmentRecord" rows="15" paginator="true"
					filteredValue="#{treatmentControler.filteredTreatmentRecord}"
					paginatorAlwaysVisible="true" paginatorPosition="bottom"
					first="#{treatmentControler.treatmentRecordListFirst}"
					paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink}">
					<p:column style="width:10%"
						headerText="#{dictionaryControler.dictionary['Seq']}">
						<h:outputText value="#{treatmentRecord.seq}" />
					</p:column>
					<p:column style="width:10%"
						filterBy="#{treatmentRecord.patientName}"
						filterMatchMode="contains"
						headerText="#{dictionaryControler.dictionary['Patient']} #{dictionaryControler.dictionary['Name']}">
						<h:outputText value="#{treatmentRecord.patientName}" />
					</p:column>
					<p:column style="width:10%"
						filterBy="#{treatmentRecord.doctorName}"
						filterMatchMode="contains"
						headerText="#{dictionaryControler.dictionary['Doctor']} #{dictionaryControler.dictionary['Name']}">
						<h:outputText value="#{treatmentRecord.doctorName}" />
					</p:column>
					<p:column style="width:10%"
						headerText="#{dictionaryControler.dictionary['Symptom']}">
						<h:outputText value="#{treatmentRecord.symptom}" />
					</p:column>
					<p:column style="width:10%"
						headerText="#{dictionaryControler.dictionary['Comment']}">
						<h:outputText value="#{treatmentRecord.doctorComment}" />
					</p:column>
					<p:column style="width:10%"
						headerText="#{dictionaryControler.dictionary['Date']}">
						<h:outputText value="#{treatmentRecord.treatDate}">
							<f:convertDateTime pattern="yyyy-MM-dd" timeZone="GMT+8" />
						</h:outputText>
					</p:column>
					<p:column style="width:3%"
						headerText="#{dictionaryControler.dictionary['Edit']}">
						<p:commandButton id="editButton" rendered="#{treatmentRecord.canEdit}"
							update=":form04:detailDialog,:form01:message"
							action="#{treatmentControler.startEditTreatmentRecord}"
							icon="ui-icon-pencil">
							<f:setPropertyActionListener value="#{treatmentRecord}"
								target="#{treatmentControler.selectedTreatmentRecord}" />
						</p:commandButton>
					</p:column>
				</p:dataTable>
			</p:panel>
			<h:outputText value="Access Denied, please contact the administrator"
				rendered="#{!(mainMenuControler.hasUserRole('admin') || mainMenuControler.hasUserRole('treatment'))}"
				style="width:100%; height:50%; text-align:center; font-size:150% !important;position:absolute; margin-left:0; margin-right:0; margin-top:0;margin-bottom:0" />
		</h:form>
	</ui:define>

	<ui:define name="dialog">
		<h:form id="form04">
			<p:dialog id="detailDialog" widgetVar="detailDialog_w" modal="true"
				showEffect="fade" resizable="false" draggable="true"
				closable="false" visible="#{treatmentControler.editOrAddMode}"
				header="#{dictionaryControler.dictionary['Treatment']} #{dictionaryControler.dictionary[treatmentControler.header]}">
				<p:panelGrid columns="4">
					<p:outputLabel
						value="#{dictionaryControler.dictionary['Patient Name']}" />
					<h:outputText rendered="#{treatmentControler.editOrAddMode}"
						value="#{treatmentControler.treatmentRecord.patientDto.name}" />

					<p:outputLabel
						value="#{dictionaryControler.dictionary['Doctor Name']}" />
					<h:outputText rendered="#{treatmentControler.editOrAddMode}"
						value="#{treatmentControler.treatmentRecord.doctorDto.name}"
						editable="false" />

					<h:outputText
						value="#{dictionaryControler.dictionary['Treatment Date']}"
						rendered="#{treatmentControler.editOrAddMode}" />
					<h:outputText
						value="#{treatmentControler.treatmentRecord.treatDate}">
						<f:convertDateTime pattern="yyyy-MM-dd" timeZone="GMT+8" />
					</h:outputText>

					<h:outputText value="#{dictionaryControler.dictionary['Symptom']}" />
					<p:inputTextarea rows="3" cols="23"
						value="#{treatmentControler.treatmentRecord.symptom}"
						maxlength="100" required="true"
						rendered="#{treatmentControler.editOrAddMode}"
						requiredMessage="#{dictionaryControler.dictionary['Symptom']} {dictionaryControler.dictionary['Required']}" />
					<h:outputText value="#{dictionaryControler.dictionary['Comment']}" />
					<p:inputTextarea rows="3" cols="23"
						value="#{treatmentControler.treatmentRecord.doctorComment}"
						maxlength="100" required="true"
						rendered="#{treatmentControler.editOrAddMode}"
						requiredMessage="#{dictionaryControler.dictionary['Comment']} {dictionaryControler.dictionary['Required']}" />
				</p:panelGrid>
				<br />
				<h:panelGrid columns="2" styleClass="datatable"
					style="margin-bottom:20px;width:900px;vertical-align:top">
					<p:dataTable id="medicineTable_t" widgetVar="medicineTable_t_w"
						style="margin-bottom:20px;width:300px;vertical-align:top"
						editable="true"
						value="#{treatmentControler.treatmentRecord.medicines}"
						var="medicine">
						<p:column style="width:10%"
							headerText="#{dictionaryControler.dictionary['Name']}">
							<h:outputText value="#{medicine.key.name}" />
						</p:column>
						<p:column style="width:10%"
							headerText="#{dictionaryControler.dictionary['Quantity']}">
							<p:cellEditor>
               					<f:facet name="output">
									<h:outputText value="#{medicine.quantity}" />
								</f:facet>
               					<f:facet name="input">
									<p:inputText id="modelInput" value="#{medicine.quantity}" />
								</f:facet>
           					</p:cellEditor>
						</p:column>

						<p:column style="width:10%">
							<p:rowEditor />
						</p:column>
					</p:dataTable>

					<p:dataTable id="medicineTable" widgetVar="medicineTable_w"
						value="#{medicineListControler.medicineList}" var="medicine"
						style="margin-bottom:20px;width:600px;vertical-align:top"
						filteredValue="#{medicineListControler.filteredTerms}">
						<p:column style="width:10%" filterBy="#{medicine.name}"
							filterMatchMode="contains"
							headerText="#{dictionaryControler.dictionary['Name']}">
							<h:outputText value="#{medicine.name}" />
						</p:column>
						<p:column style="width:10%" filterBy="#{medicine.genericNumber}"
							filterMatchMode="contains"
							headerText="#{dictionaryControler.dictionary['Generic Number']}">
							<h:outputText value="#{medicine.genericNumber}" />
						</p:column>
						<p:column style="width:10%"
							headerText="#{dictionaryControler.dictionary['Note']}">
							<h:outputText value="#{medicine.note}" />
						</p:column>
						<p:column style="width:10%"
							headerText="#{dictionaryControler.dictionary['Price']}">
							<h:outputText value="#{medicine.price}" />
						</p:column>
						<p:column style="width:3%"
							headerText="#{dictionaryControler.dictionary['Add']}">
							<p:commandButton id="editButton"
								update=":form05:addMedicineDialog,:form04:msg"
								action="#{treatmentControler.startAddMedicine}"
								icon="ui-icon-pencil" immediate="true">
								<f:setPropertyActionListener value="#{medicine}"
									target="#{treatmentControler.selectedMedicine}" />
							</p:commandButton>
						</p:column>
					</p:dataTable>
				</h:panelGrid>
				<p:messages id="msg" />
				<div style="text-align: right">
					<p:commandButton value="#{dictionaryControler.dictionary['Save']}"
						ajax="true"
						actionListener="#{treatmentControler.saveTreatmentRecord(true)}"
						update="detailDialog,:form01:treatmentRecordTable"
						oncomplete="PF('treatmentRecord_w').clearFilters();"
						style="margin-right:5px" />
					<p:commandButton value="#{dictionaryControler.dictionary['Save']} #{dictionaryControler.dictionary['Temporarily']}"
						ajax="true"
						actionListener="#{treatmentControler.saveTreatmentRecord(false)}"
						update="detailDialog,:form01:treatmentRecordTable"
						oncomplete="PF('treatmentRecord_w').clearFilters();"
						style="margin-right:5px" />
					<p:commandButton id="cancelBtn"
						value="#{dictionaryControler.dictionary['Cancel']}" ajax="true"
						actionListener="#{treatmentControler.cancelSaveTreatmentRecord}"
						update="detailDialog" immediate="true" />
				</div>
			</p:dialog>
		</h:form>
		<h:form id="form05">
			<p:dialog id="addMedicineDialog" widgetVar="addMedicineDialog_w"
				modal="true" showEffect="fade" resizable="false" draggable="true"
				closable="false" visible="#{treatmentControler.addMedicineMode}"
				header="#{dictionaryControler.dictionary['Add']} #{dictionaryControler.dictionary['Medicine']}">

				<p:panelGrid columns="4">
					<p:outputLabel
						value="#{dictionaryControler.dictionary['Medicine']} #{dictionaryControler.dictionary['Name']}" />
					<h:outputText value="#{treatmentControler.selectedMedicine.name}" />
					<p:outputLabel
						value="#{dictionaryControler.dictionary['Quantity']}" />
					<p:inputText value="#{treatmentControler.medicineQuantity}" />
				</p:panelGrid>
				<div style="text-align: right">
					<p:commandButton value="#{dictionaryControler.dictionary['Add']}"
						ajax="true" actionListener="#{treatmentControler.addMedicine}"
						update=":form05:addMedicineDialog,:form04:medicineTable_t"
						style="margin-right:5px" />
					<p:commandButton
						value="#{dictionaryControler.dictionary['Cancel']}" ajax="true"
						actionListener="#{treatmentControler.cancelAddMedicine}"
						update=":form05:addMedicineDialog" immediate="true" />
				</div>
				<p:messages />
			</p:dialog>
		</h:form>
	</ui:define>
</ui:composition>
</html>